import * as Cesium from "cesium";

let viewer: Cesium.Viewer;
export const cesiumInit = (
  container: Element | string,
  options: Cesium.Viewer.ConstructorOptions = {}
) => {
  viewer = new Cesium.Viewer(
    container,
    Object.assign(
      {
        timeline: false, // 时间轴控件 (展示当前时间和允许用户在进度条上拖动到任何一个指定的时间) 默认true
        animation: false, //动画控件( 隐藏动画速度控制器) (控制视窗动画的播放速度) 默认true
        geocoder: false, // 搜索按钮（隐藏查找位置） 默认true
        homeButton: false, //主页按钮（隐藏返回视角到初始位置） 默认true
        sceneModePicker: false, //投影方式按钮 (切换2D、3D 和 Columbus View (CV) 模式) 默认true
        baseLayerPicker: false, //图层选择按钮 (择三维数字地球的底图（imagery and terrain)) 默认true
        navigationHelpButton: false, //帮助手势按钮 默认true
        fullscreenButton: false, //全屏按钮 默认true
        shadows: true, //是否显示背影
        infoBox: false, // 右侧信息框  homeButton
        selectionIndicator: false, //隐藏选中状态
        shouldAnimate: true, // 这个值比较特殊，如果为true页面渲染后自动执行动画，反之需要自己手动去点击开始
        orderIndependentTranslucency: false, //透明必须设置true
      },
      options
    )
  );
  console.log("%c🌏", `font-size: 100px;`);
  tag("cesium版本", (Cesium as any).VERSION);
  (viewer.cesiumWidget.creditContainer as HTMLDivElement).style.display =
    "none";
  const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  // 鼠标左键
  handler.setInputAction((e: any) => {
    const picked = viewer?.scene.pick(e.position);
    // 判断是否拾取到模型
    if (!picked) {
      return;
    }
    if (picked?.id?._node) {
      picked.id._node.click(picked.id, picked.id.data);
    }
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  /*
    mouseEnter
mouseLeave
  */
  // 监听鼠标移出事件
  // handler.setInputAction((movement: any) => {
  //   console.log(movement);

  //   const startPosition = viewer.scene.pick(movement.startPosition);
  //   const endPosition = viewer.scene.pick(movement.endPosition);
  //   // console.log('在这里处理鼠标移入事件-->', Cesium.defined(pickedObject), pickedObject);
  //   // console.log('在这里处理鼠标移出事件-->', !Cesium.defined(pickedObject), pickedObject);
  //   if (Cesium.defined(endPosition)) {
  //     console.log('Mouse left', endPosition.id);
  //     // 在这里处理鼠标移出事件
  //   }
  //   if (Cesium.defined(startPosition)) {
  //     console.log('Mouse entered:', startPosition.id._node);
  //     // 在这里处理鼠标移入事件
  //   }
  // }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  Cesium.JulianDate.fromDate(new Date());
};
export const getViewer = (): Cesium.Viewer => viewer;
const tag = (key: any, value: any) => {
  console.log(
    `%c ${key} %c ${value} `,
    "color:white;background-color:#5f5f5f;padding: 3px;border-radius: 5px 0px 0px 5px;;font-family: 'pingfang SC','helvetica neue',arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif;",
    "color:white;background-color:#1384c4;border-radius: 0px 5px 6px 0px;;font-family: 'pingfang SC','helvetica neue',arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif;padding: 3px;"
  );
};
